﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace School.Controls
{
    public partial class DownloadFileList : System.Web.UI.UserControl
    {
        private SqlDataReader sdr = null;
        public SqlDataReader DataSource
        {
            get { return sdr; }
            set { sdr = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rep_DownloadFileItem.DataSource = sdr;
            rep_DownloadFileItem.DataBind();
        }

        protected string FileSizeConvert(long size)
        {
            string result = "0 Bytes";
            if (size < 1)
            {
                result = string.Format("{0} Bytes", Math.Round((double)size, 2));
            }
            else if (size >= 1 && size < 1024)
            {
                result = string.Format("{0} Bytes", Math.Round((double)size, 2));
            }
            else if (size >= 1024 && size < 1024 * 1024)
            {
                result = string.Format("{0} KB", Math.Round((double)size / 1024, 2));
            }
            else if (size >= 1024 * 1024 && size < 1024 * 1024 * 1024)
            {
                result = string.Format("{0} MB", Math.Round((double)size / (1024 * 1024), 2));
            }
            else if (size >= 1024 * 1024 * 1024 && size < (long)1024 * 1024 * 1024 * 1024)
            {
                result = string.Format("{0} GB", Math.Round((double)size / (1024 * 1024 * 1024), 2));
            }
            else
            {
                result = string.Format("{0} TB", Math.Round((double)size / ((double)1024 * 1024 * 1024 * 1024), 2));
            }

            return result;
        }

        protected void rep_DownloadFileItem_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                string[] args = e.CommandArgument.ToString().Split(',');
                Response.Redirect("~/WebPages/Download/DownloadExecute.aspx?FID=" + args[0] + "&FilePath=" + args[1]);
            }
        }
    }
}